﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;
using System.Collections;

namespace PsychveyApp1
{
    public partial class CreateSurvey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUsername.Text = (string)(Session["username"]);
        }


        protected void tbxSurveyTitle_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnProceed_Click(object sender, EventArgs e)
        {
            if (ddlUser.SelectedIndex == 1)
            {

                Response.Redirect("UserAccountHome.aspx");
            }
            else if (ddlUser.SelectedIndex == 2)
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void btnCreateSurvey_Click(object sender, EventArgs e)
        {

            Survey survey = new Survey();
            survey.dateCreated = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "-" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            survey.userID = ((UserAccount)Session["currentUser"]).userID;
            survey.dateModified = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "-" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            survey.title = tbxSurveyTitle.Text;
            //call getcurrentmaxidnumber according to the currentuser
            CurrentMaxIDNumber currentMaxIDNumber = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
            //increment surveyid
            currentMaxIDNumber.currentMaxSurveyID++;
            survey.surveyID = ((UserAccount)Session["currentUser"]).username + "__SRVY" + currentMaxIDNumber.currentMaxSurveyID;
            survey.expirationDateOfSurvey = "";
            survey.noOfRespondents = "";
            //update surveyid to database
            DBManager.UpdateCurrentMaxIDNumber(currentMaxIDNumber);
            survey.typeOfSurvey = tbxTypeOfSurvey.Text;
            DBManager.InsertSurvey(survey);


            string surveytitle = tbxSurveyTitle.Text;
            string typeofsurvey = tbxTypeOfSurvey.Text;

            string querystring = "surveytitle=" + surveytitle;
            querystring += "&" + "typeofsurvey=" + typeofsurvey;

            Survey survey1 = new Survey();
            survey1.title = surveytitle;
            survey1.typeOfSurvey = typeofsurvey;
            survey1.dateCreated = survey.dateCreated;
            survey1.dateModified = survey.dateModified;
            survey1.userID = survey.userID;
            survey1.typeOfSurvey = survey.typeOfSurvey;
            survey1.surveyID = survey.surveyID;
            survey1.expirationDateOfSurvey = survey.expirationDateOfSurvey;
            survey1.noOfRespondents = survey.noOfRespondents;

            //then session[""] = survey
            Session["currentSurvey"] = survey1;
            Session["currentPageNumber"] = 1;
            Response.Redirect("InformationSheet.aspx?" + querystring);
            //Server.Transfer("CreateSurvey2.aspx?" + querystring);
            //Server.TransferRequest("InformationSheet.aspx?" + querystring);
            //create survey with all the values
            
            


        }

        protected void AdminLoginShortcut_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLogin.aspx");
        }

        protected void MySurveyLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MySurvey.aspx");
        }

    }
}
